//
//  HMTabBarController.swift
//  SinaWeibo
//
//  Created by apple on 16/9/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //将UITabBarController 的属性 tabBar替换成自定义的tabbar
        //只读属性不能够直接设置值 可以通过KVC间接的设置值
        let hmTabBar = HMMainTabBar()
        
        
        //实现闭包
        hmTabBar.composeClosure = { [weak self] in
            print("撰写按钮被点击",self)
            print("~~~~~~~~~~~~~~~~~~~~~~")
        }
        
        
        //hmTabBar.composeBtn.addTarget(<#T##target: Any?##Any?#>, action: <#T##Selector#>, for: <#T##UIControlEvents#>)
        
        
        self.setValue(hmTabBar, forKey: "tabBar")
        //添加子视图控制器
        addChildViewControllers()
        //修改默认的颜色
        //tabBar.tintColor = UIColor.orange

    }
    
    
    //MARK: 添加控制器
    private func addChildViewControllers() {
        addChildViewController(vc: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(vc: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: HMProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        //设置title
        //home.title = "首页"
        vc.navigationItem.title = title
        //要同时设置title 和图片才可以显示
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中图片
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        //vc.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        //设置字体
        //vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 11)], for: .normal)
        //设置偏移
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
        //修改徽标的颜色
        vc.tabBarItem.badgeColor = UIColor.purple
        //不显示
        vc.tabBarItem.badgeValue = nil
        let nav = HMBaseNavController(rootViewController: vc)
        // addChildViewController: 如果在自定义tabBar 将tabBar上的tabBarButton隐藏 自己添加自定义按钮 并且还使用该方法 就会楚翔 自定义按钮无法点击, 使用 setViewControllers: animated: 就可以解决该问题
        addChildViewController(nav)
        
    }
    
    

}
